package com.ruoyi.classai.api;

import com.ruoyi.classai.domain.*;
import com.ruoyi.classai.domain.dto.AnalysisModeRelDto;
import com.ruoyi.classai.service.*;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.system.api.RemoteAiModelService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.UUID;

import static com.ruoyi.common.core.utils.PageUtils.startPage;

/**
 * 用户登录接口
 * @author zl
 * @date 2025年02月27日 11:56
 */
@Slf4j
@Validated
@RestController
@Tag(name = "视频模型接口", description = "视频模型接口")
@RequestMapping("/api/model")
public class AnalysisModeApi extends BaseController{
    @Autowired
    private AnalysisModeRelService analysisModeRelService;
    @Autowired
    private EwcAnalysismodeRelService ewcAnalysismodeRelService;
    @Autowired
    private IEwcCourseGroupingService iEwcCourseGroupingService;
    @Autowired
    private IEwcHomeworkSubmissionMaterialsService ewcHomeworkSubmissionMaterialsService;
    @Autowired
    private RemoteAiModelService remoteAiModelService;

    @PostMapping("/Notification")
    public AjaxResult modelNotification(@RequestBody AnalysisModeRelDto analysisModeRelDto) {
        log.info("收到大模型通知请求，请求时间：{}，请求内容：{}", new Date(), analysisModeRelDto.toString());
        if (analysisModeRelDto.getCode().equals("500")) {
            AnalysisModeRel analysisModeRel = new AnalysisModeRel();
            analysisModeRel.setTask_id(analysisModeRelDto.getTask_id());
            analysisModeRel.setAnalysis_msg(analysisModeRelDto.getAnalysis_msg());
            ewcHomeworkSubmissionMaterialsService.updateByFileNameForFail(analysisModeRel);
            //更新ewc_course_grouping 课程分组表
            ////通过id更新
            iEwcCourseGroupingService.updateByFileNameForFai(analysisModeRel);
        } else {
            AnalysisModeRel analysisModeRel = new AnalysisModeRel();
            analysisModeRel.setTask_id(analysisModeRelDto.getTask_id());
            analysisModeRel.setScore(analysisModeRelDto.getScore());
            analysisModeRel.setAnalysis_msg(analysisModeRelDto.getAnalysis_msg());
            analysisModeRel.setFilePath(analysisModeRelDto.getFilePath());
            analysisModeRel.setOriginal_filepath(analysisModeRelDto.getOriginal_filepath());
            analysisModeRel.setSynthesisSpeed(analysisModeRelDto.getSynthesis().getSpeed());
            analysisModeRel.setSynthesisGesture(analysisModeRelDto.getSynthesis().getGesture());
            analysisModeRel.setSynthesisRhythm(analysisModeRelDto.getSynthesis().getRhythm());
            analysisModeRel.setType(analysisModeRelDto.getType());


            try {
                // 根据 task_id 更新记录   ewc_analysismode_rel 视频模型分析关联表
                boolean success = analysisModeRelService.updateByTaskId(analysisModeRel);
                //获取学习信息，并获取模板内容
                QueryAiMsg queryAiMsg = iEwcCourseGroupingService.queryAiMsg(analysisModeRel);
               if (queryAiMsg == null) {
                   log.warn("未查询到对应的学习信息，无法生成AI建议");
                   return AjaxResult.warn("更新成功，但未生成AI建议");
               }
                String msgTemplate = queryAiMsg.getAiTemplate();
                msgTemplate = msgTemplate.replace("@course@", queryAiMsg.getCourseName());
                msgTemplate = msgTemplate.replace("@capter@", queryAiMsg.getChapterName());
                msgTemplate = msgTemplate.replace("@studentgender@", queryAiMsg.getGender());
                msgTemplate = msgTemplate.replace("@birthdate@", queryAiMsg.getAge());
                msgTemplate = msgTemplate.replace("@aidescription@", analysisModeRel.getAnalysis_msg());
                String aiResult = "";
                if (analysisModeRelDto.getCode().equals("200")) {
                    aiResult = remoteAiModelService.chat(msgTemplate, String.valueOf(UUID.randomUUID()));
                }
                ;
                log.info("aimodel:" + aiResult);

                //获取文件原路径
//            EwcAnalysismodeRel byTaskId = ewcAnalysismodeRelService.getByTaskId(analysisModeRel);
//            analysisModeRel.setOriginal_filepath(byTaskId.getOriginalFilepath());
                //更新ewc_homework_submission_materials 作业提交资料表
                //通过id更新
                ewcHomeworkSubmissionMaterialsService.updateByFileName(analysisModeRel, aiResult);
                //更新ewc_course_grouping 课程分组表
                ////通过id更新
                iEwcCourseGroupingService.updateByFileName(analysisModeRel, aiResult);


                if (success) {
                    return AjaxResult.success("更新成功");
                } else {
                    return AjaxResult.error("更新失败");
                }
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.error("更新过程中出现异常");
            }
        }
        return AjaxResult.success("更新成功");
    }



    }

















